<?php
/**
 * Created by PhpStorm.
 * User: yangxu
 * Date: 2016/5/4
 * Time: 9:56
 */

namespace Admin\Controller;
use Think\Controller;

class AdsManageController extends AdminController{
    private $cat;
    public function __construct(){
        parent::__construct();
        if(!logAdmin(session('adminId'))) header('location:'.U('Admin/Index/login'));
        $this->cat=M('Category');
    }
    public function index(){
        $list=$this->cat->select();
        //$catList=$this->_list();
        vendor('Tree');
        $tree=new \Tree();
        $tree->tree($list);
        $catList=$tree->getRet();
//        echo '<pre>';
//        print_r();
//        echo '</pre>';
        $this->assign('catList',$catList);
        $this->display('AdsManage/IndexCover');
    }
    public function Cat_List(){
        $map['level']=2;
        $list=$this->cat->where($map)->select();
        $this->assign('catList',$list);
        $this->display();
    }
    
    public function AdsManage_cata_list(){
        if(IS_GET){
            $type=I('get.level_one',1);
            $result=D('Adsmanage')->getCata($type);
            $CataOne=$this->getCataOne();
            $this->assign('CataOne',$CataOne);
            $this->assign('level_one_hid',$type);
            $this->assign('cata_list',$result);
            $this->display('AdsManage/Cat_List');
        }
    
    }
    
    public function getCataOne(){
        $result=M('category')->where('level=1')->select();
        if($result){
            return $result;
        }
    }
    
    public function editCat(){
             if($_GET){  
                 $Cat_id=I('get.id');
                 if($Cat_id){
                            $map['cat_id']=$Cat_id;
                            $result=M('adsmanage')->where($map)->find();
                            if($result){
                                $this->assign('cover',$result['cover']);
                                $this->assign('link',$result['link']);
                                $this->assign('id',$Cat_id);
                                $this->display('edit');
                            }else{
                                $data['cat_id']=$Cat_id;
                                M('adsmanage')->data($data)->add();//广告表中没有记录时添加记录
                                $this->assign('id',$Cat_id);
                                $this->display('edit');
                            }
                 }
             }
    }
    public function save(){
        if(IS_POST){
           $data=I('post.');
           $map['cat_id']=$data['id'];
           $id=M('adsmanage')->where($map)->field('cover')->find();
           $string=$id['cover'];
           if(!empty($string)){
           $r=explode('/',$string);
           $dir=dirname(dirname(dirname(dirname(__FILE__)))).'\Upload\pic_Ads\\'.$r[4];
           unlink($dir);//删除之前存储在upload文件中的图片。
           }
           $temp['link']=$data['link'];
           $temp['cover']=$data['cover'];
           $map['cat_id']=$data['id'];
           $result=M('adsmanage')->where($map)->save($temp);
           if(0<=$result)$this->ajaxReturn('保存成功');
           else $this->ajaxReturn('保存失败');
        }
    }

    public function _list($parent_id=0){
        $catList=array();
        $list=$this->cat->where('parent_id='.$parent_id)->select();
        foreach($list as $key => $val){
            $val['son']=$this->_list($val['id']);
            $catList[]= $val;
        }
        return $catList;
    }
}